package org.xnsg.app.core.intercept;

/**
 * @author zhaopan
 * @date :2017/4/16
 * @Description:
 */

import com.eova.model.User;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.JsonKit;
import org.xnsg.app.core.HttpCode;
import org.xnsg.app.core.bean.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class LoginInterceptor implements Interceptor {
    public static ArrayList<String> excludes = new ArrayList();

    static {
        excludes.add("/");
        //excludes.add("/toLogin");
        //excludes.add("/vcodeImg");
        excludes.add("/login/doLogin");
        //excludes.add("/doInit");
        //excludes.add("/toTest");
        excludes.add("/form");
        excludes.add("/doForm");
        //excludes.add("/upgrade");
        //excludes.add("/why");
    }

    public LoginInterceptor() {
    }

    public void intercept(Invocation inv) {
        if(excludes.contains(inv.getActionKey())) {
            inv.invoke();
        } else {
            User user = (User)inv.getController().getSessionAttr("user");
            if(user == null) {
                //inv.getController().redirect("/toLogin");
                Map resultMap= new HashMap();
                resultMap.put("httpCode", HttpCode.UNAUTHORIZED);
                resultMap.put("msg", "回话已经过期");
                String json = JsonKit.toJson(new Result(resultMap));
                inv.getController().renderJson(json);
            } else {
                inv.invoke();
            }
        }
    }
}
